package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 处理格局用户id查询用户购物车的的请求
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //从线程中获取到用户id
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .list();
        return R.success(list);
    }

    /**
     * 处理添加商品到购物车的请求
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //从线程中获取到用户id
        Long userId = BaseContext.getCurrentId();
        //判断用户购物车中是否包含该商品
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if (one != null) {
            //说明购物车已有该商品,数量+1
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            return R.success(one);
        } else {
            //否则,表示购物车中没有该商品,直接添加
            //设置用户id
            shoppingCart.setUserId(userId);
            //设置数量为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 处理根据用户id清空购物车的请求
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete() {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCartService.lambdaUpdate().eq(ShoppingCart::getUserId, userId).remove();
        return R.success("清空成功");
    }

    /**
     * 处理根据商品id修改商品数量的请求
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R update(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long userId = BaseContext.getCurrentId();
        //判断购物车中商品数量是否大于1
        ShoppingCart one = shoppingCartService.lambdaQuery()
                .eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .one();
        if (one.getNumber() > 1) {
            //说明购物车中该商品不止一件,-1
            one.setNumber(one.getNumber() - 1);
            shoppingCartService.updateById(one);
        } else {
            //否则就说明购物车中该商品只剩一件或没有,直接删除
            shoppingCartService.removeById(one.getId());
            one.setNumber(0);
        }
        return R.success(one);
    }
}
